﻿using System;
using System.Web;
using System.Text;

namespace MikeKappel.Com.CS
{
    public static partial class Helper
    {
        public enum RandomType
        {
            AlfaNumericUpper,
            AlfaNumericLower,
            AlfaNumericMixed
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="maxSize"></param>
        /// <param name="availableChars"></param>
        /// <returns></returns>
        public static string Random(Int32 maxSize, String availableChars)
        {
            char[] chars = new char[62];
            chars = availableChars.ToCharArray();
            byte[] data = new byte[1];
            System.Security.Cryptography.RNGCryptoServiceProvider crypto = new System.Security.Cryptography.RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[maxSize];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(maxSize);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length - 1)]);
            }
            return result.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="maxSize"></param>
        /// <returns></returns>
        public static string Random(Int32 maxSize, RandomType type)
        {
            String availableChars = "";
            if (type == RandomType.AlfaNumericLower)
                availableChars = "abcdefghijklmnopqrstuvwxyzZ1234567890";
            else if (type == RandomType.AlfaNumericUpper)
                availableChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            else if (type == RandomType.AlfaNumericMixed)
                availableChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

            return Random(maxSize, availableChars);
        }

	}
}
